﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MPMusicVids.Importer;
using MPMusicVids.Data;
using MediaPortal.Configuration;

namespace MPMusicVids
{
    public partial class Manual : Form
    {
        ArtistSearch search = new ArtistSearch();
        Artist thisArtist;
        string[] results, selectedArtist;
        DataManager dm = MusicVidsMain.dm;

        public Manual()
        {
            InitializeComponent();
        }

        private void LoadBox(string[] results)
        {
            mtchList.Items.Clear();
            foreach (string result in results)
            {
                mtchList.Items.Add(result.Split(';')[0]);
            }
        }

        private void LoadArtist()
        {
            nameText.Text = thisArtist.ToString();
            bioText.Text = thisArtist.Bio;
        }

        private void mtchList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = mtchList.SelectedIndex;
            string selArtist = results[i];
            selectedArtist = search.GetArtistInfo(selArtist.Split(';')[1], selArtist.Split(';')[0]);
            thisArtist = new Artist("0", selectedArtist[0], selectedArtist[1], selectedArtist[2]);
            LoadArtist();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            //Save the artist details
            if (thisArtist.Image == null)
                thisArtist.Image = "";
            int ArtistID = dm.addArtist(thisArtist.ToString(), thisArtist.Bio, thisArtist.Image);
            ListBox.SelectedIndexCollection indices = fileList.SelectedIndices;
            ArrayList toGo = new ArrayList();
            foreach (int i in indices)
            {
                MusicVideo tmpVideo = (MusicVideo)fileList.Items[i];
                toGo.Add(tmpVideo);
                tmpVideo.ArtistID = ArtistID.ToString();
                tmpVideo.Thumbnail = getThumbnail(tmpVideo);
                tmpVideo.Save();
            }
            foreach (MusicVideo i in toGo)
                fileList.Items.Remove(i);

        }

        private string getThumbnail(MusicVideo video)
        {
            string imgFilename = Config.GetFile(Config.Dir.Thumbs, @"Music Vids\Thumbs\" + thisArtist.ToString() + " - " + video.Name + ".jpg");
            string args = "-y -i \"" + video.File + "\" -an -ss " + dm.getSeconds() + " -an -r 1 -vframes 1 -y -vcodec mjpeg -f mjpeg \"" + imgFilename + "\"";
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.Arguments = args;
            proc.StartInfo.FileName = dm.getFFMPEG();
            proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            proc.Start();
            System.Threading.Thread.Sleep(200);
            return imgFilename;
        }

        private void srchButton_Click(object sender, EventArgs e)
        {
            results = search.GetArtists(searchText.Text);
            LoadBox(results);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Open browse for files dialog. Put all in listbox
            OpenFileDialog selectFiles = new OpenFileDialog();
            selectFiles.Multiselect = true;
            selectFiles.Title = "Select music videos for manual importing";
            selectFiles.Filter = "All Files|*.*";
            selectFiles.FilterIndex = 1;
            string[] files = null;
            if (selectFiles.ShowDialog() != DialogResult.Cancel)
            {
                files = selectFiles.FileNames;
                foreach (string file in files)
                {
                    MusicVideo toAdd = new MusicVideo();
                    toAdd.File = file;
                    toAdd.Name = file.Substring((file.LastIndexOf('\\')+1), (file.Length - file.LastIndexOf('\\')-1));
                    fileList.Items.Add(toAdd);
                }
            }

        }

        private void nameText_TextChanged(object sender, EventArgs e)
        {
            if (thisArtist != null)
            {
                thisArtist.Name = nameText.Text;
            }
            else
            {
                thisArtist = new Artist();
                thisArtist.Image = "";
                thisArtist.Name = nameText.Text;
            }
        }

        private void bioText_TextChanged(object sender, EventArgs e)
        {
            if (thisArtist != null)
            {
                thisArtist.Bio = bioText.Text;
            }
            else
            {
                thisArtist = new Artist();
                thisArtist.Image = "";
                thisArtist.Bio = bioText.Text;
            }
        }        
        
    }
}
